package com.yanqu.road.server.manager.activity.xingyou.inter.grid;

import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiTeamActionLogData;
import com.yanqu.road.entity.activity.youxing.ShanShuiGridParam;
import com.yanqu.road.entity.activity.youxing.config.XingYouShanShuiGridInfo;
import com.yanqu.road.entity.activity.youxing.config.XingYouShanShuiRoleInfo;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.manager.activity.xingyou.DiceHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.enums.eXingYouGridTypes;
import com.yanqu.road.server.manager.activity.xingyou.inter.GridHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.inter.IXingYouGridHandler;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

/**
 * 大内
 */
public class DaNeiGridHandler implements IXingYouGridHandler {

    @Override
    public void handle(XingYouShanShuiGridInfo gridInfo, int pos, int gridId, int endPos, boolean fly, DiceHandleResult result) {

        //是否停留
        boolean stop = pos == endPos;
        if(stop){

        }

        //起始变成银票
        if(result.getUserData().isDrift()){
            XingYouShanShuiGridInfo info = result.getActivity().getConfig().getGridInfo(eXingYouGridTypes.Three.getId());

            XingYouShanShuiProto.ShanShuiEventInfo.Builder builder = XingYouShanShuiProto.ShanShuiEventInfo.newBuilder();
            builder.setPosition(pos);
            builder.setGridId(gridId);
            String reward = result.getConfig().getSHANSHUI_SILVER_COIN_ID() + "=" + info.getParam().get(0);

            Property property = PropertyHelper.parseStringToProperty(reward);

            Property fixedReward = result.getActivity().getGridInfoFixedReward(result.getUserData(), gridInfo, stop);
            builder.setFixedReward(PropertyHelper.parsePropertyToString(fixedReward));

            property.addProperty(fixedReward);

            builder.setAward(PropertyHelper.parsePropertyToString(property));

            GridHandleResult gridHandleResult = new GridHandleResult();
            gridHandleResult.setBuilder(builder);

            ShanShuiGridParam gridParam = new ShanShuiGridParam(gridId, pos, true, result.getUserData().isDrift());
            gridHandleResult.setGridParam(gridParam);
            result.getGridHandleResultList().add(gridHandleResult);

            //这边逻辑应该不会执行
            if(stop) {
                result.getActivity().tryCatch(pos, result.getUserData(), result, gridHandleResult);
            }
            return;
        }

        GridHandleResult gridHandleResult = new GridHandleResult();
        Property property = new Property();
        if(result.getUserData().getRoleId() == 5){
            XingYouShanShuiRoleInfo roleInfo = result.getConfig().getRoleInfo(5);
            //专属骰子
            property.addProperty(result.getConfig().getSHANSHUI_EXCLUSIVE_DICE_ID(), roleInfo.getSkillParam().get(0));
        }
        XingYouShanShuiProto.ShanShuiEventInfo.Builder builder = XingYouShanShuiProto.ShanShuiEventInfo.newBuilder();
        property.addProperty(result.getConfig().getSHANSHUI_SILVER_COIN_ID(), gridInfo.getParam().get(0));

        int roleBuffAddition = result.getActivity().getRoleBuffAddition(result.getUserData(), 3);
        //事件跳转的
        if(result.isEventGridJump()){
            roleBuffAddition += result.getActivity().getRoleBuffAddition(result.getUserData(), 2);
        }
        builder.setBaseReward(PropertyHelper.parsePropertyToString(property));

        result.getActivity().calRewardBuff(result.getUserData(), property, result.isInMazu(), roleBuffAddition);

        //队伍日志：经过大内
        CrossXingYouShanShuiTeamActionLogData actionLogData = result.getActivity().createActionLogData(result.getUserData(), XingYouShanShuiProto.ShanShuiLogTypeEnum.SHAN_SHUI_LOG_START,
                PropertyHelper.parsePropertyToString(property), String.valueOf(result.getUserData().getRoleId()));


        if(stop && !StringUtils.isNullOrEmpty(gridInfo.getReward3())) {
            Property fixedReward = RandomWeightConfigHelper.getProperty(gridInfo.getReward3());
            property.addProperty(fixedReward);
            builder.setFixedReward(PropertyHelper.parsePropertyToString(fixedReward));
        }

        builder.setPosition(pos);
        builder.setGridId(gridId);
        builder.setAward(PropertyHelper.parsePropertyToString(property));
        gridHandleResult.setBuilder(builder);

        ShanShuiGridParam gridParam = new ShanShuiGridParam(gridId, pos, true, result.getUserData().isDrift());
        gridHandleResult.setGridParam(gridParam);
        result.getGridHandleResultList().add(gridHandleResult);

        result.getActionLogDataList().add(actionLogData);
    }

}
